#include "../include/eGaduList.h"

using namespace std;

using namespace eGaduLib;

User **list = 0;
int listLen = 0;

eGaduList::eGaduList()
{
}

eGaduList::~eGaduList()
{
}

int eGaduList::getLength()
{
    return listLen;
}

void eGaduList::addUser(User *usr)
{
    if(listLen > 0)
    {
        User **tmpList = new User*[listLen + 1];
        for(int i = 0; i < listLen; i++)
        {
            tmpList[i] = new User(list[i]->getUin(), list[i]->getName(), list[i]->getType());
            tmpList[i]->setStatus(list[i]->getStatus());
            if(list[i]->getDesc() != 0)
            {
                tmpList[i]->setDesc(list[i]->getDesc());
            }
        }

        for(int i = 0; i < listLen; i++)
        {
            list[i]->release();
            delete list[i];
        }
        delete [] list;

        list = tmpList;

        listLen++;

        list[listLen - 1] = new User(usr->getUin(), usr->getName(), usr->getType());
        list[listLen - 1]->setStatus(usr->getStatus());
        if(usr->getDesc() != 0)
        {
            list[listLen - 1]->setDesc(usr->getDesc());
        }
    }
    else
    {
        list = new User*[1];
        list[0] = new User(usr->getUin(), usr->getName(), usr->getType());
        list[0]->setStatus(usr->getStatus());
        if(usr->getDesc() != 0)
        {
            list[0]->setDesc(usr->getDesc());
        }

        listLen++;
    }
}

User *eGaduList::getUser(int index)
{
    if(list != 0 && listLen > 0)
    {
        return list[index];
    }
    else
    {
        return 0;
    }
}

void eGaduList::remUser(int index)
{
    if(listLen > 0)
    {
        if(listLen == 1)
        {
            list[0]->release();
            delete list[0];
            delete [] list;
            listLen--;
        }

        if(listLen > 1)
        {
            User **tmpList = new User*[listLen - 1];
            for(int i = 0; i < (listLen - 1); i++)
            {
                tmpList[i] = new User(list[i]->getUin(), list[i]->getName(), list[i]->getType());
                tmpList[i]->setStatus(list[i]->getStatus());
                if(list[i]->getDesc() != 0)
                {
                    tmpList[i]->setDesc(list[i]->getDesc());
                }
            }

            for(int i = 0; i < listLen; i++)
            {
                list[i]->release();
                delete list[i];
            }
            delete [] list;

            list = tmpList;
        }
    }
}

void eGaduList::release()
{
    if(listLen > 0)
    {
        for(int i = 0; i < listLen; i++)
        {
            list[i]->release();
            delete list[i];
        }

        delete [] list;

        list = 0;
        listLen = 0;
    }
}

